package org.hqs.admin.controller;

import org.hqs.admin.common.JsonResult;
import org.hqs.admin.pojo.Admin;
import org.hqs.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {

    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @GetMapping("/{adminId}")
    public JsonResult<Admin> getAdminById(@PathVariable Long adminId) {
        return new JsonResult<>(adminService.queryAdminById(adminId));
    }

    @GetMapping("/findAll")
    public JsonResult<List<Admin>> findAllAdmins() {
        return new JsonResult<>(adminService.findAllAdmins());
    }

    @PostMapping("/add")
    public JsonResult addAdmin(@RequestBody Admin admin) {
        int result = adminService.insertAdmin(admin);
        if (result > 0) {
            return new JsonResult("200",  "新增管理员成功");
        } else {
            return new JsonResult("400", "新增管理员失败");
        }
    }

    @PutMapping("/update")
    public JsonResult updateAdmin(@RequestBody Admin admin) {
        int result = adminService.updateAdmin(admin);
        if (result > 0) {
            return new JsonResult("200",  "更新管理员成功");
        } else {
            return new JsonResult("400", "更新管理员失败");
        }
    }

    // 删除管理员信息
    @DeleteMapping("/delete")
    public JsonResult deleteAdmin(@RequestBody Admin admin) {
        int result = adminService.deleteAdmin(admin);
        if (result > 0) {
            return new JsonResult("200",  "删除管理员成功");
        } else {
            return new JsonResult("400", "删除管理员失败");
        }
    }
}
